package org.openkoala.koala.deploy.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
import org.openkoala.koala.deploy.action.RestWebServiceDeployAction;
import org.openkoala.koala.deploy.dialog.MethodConfigDialog;
import org.openkoala.koala.deploy.pojo.InterfaceTree;
import org.openkoala.koala.deploy.pojo.Itree;
import org.openkoala.koala.deploy.pojo.LocalRemoteTree;
import org.openkoala.koala.deploy.pojo.MethodTree;
import org.openkoala.koala.deploy.tree.action.WebServiceTreeActionGroup;
import org.openkoala.koala.deploy.webservice.pojo.InterfaceObj;
import org.openkoala.koala.deploy.webservice.pojo.RestWebServiceMethod;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceDeploy;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceMethod;
import org.openkoala.koala.java.JavaManagerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestWebServiceSelectionUI extends SelectionUI<WebServiceDeploy> {

	
	private static final Logger logger = LoggerFactory
			.getLogger(RestWebServiceSelectionUI.class);
	
	private RestWebServiceDeployAction action;
	
	private WebServiceDeploy webServiceDeploy;

	private Button methodConfig;
	
	public RestWebServiceSelectionUI(Shell shell,
			RestWebServiceDeployAction webServiceDeployAction) {
		super(shell,"发布WebService服务");
		this.action = webServiceDeployAction;
		
//		WebServiceTreeActionGroup actionGroup = new WebServiceTreeActionGroup(treeViewer);// 生成一个ActionGroup对象
//		actionGroup.fillContextMenu(new MenuManager());// 将按钮注入到菜单对象中
		
		createMethodConfigButton();
		addListenerForMethodconfigButton();
	}

	private void createMethodConfigButton() {
		methodConfig = new Button(shell, SWT.PUSH);
		methodConfig.setText("Method Config");
		FormData data = new FormData();
		data.left = new FormAttachment(deSelectAll, 0, SWT.LEFT);
		data.top = new FormAttachment(deSelectAll, 15);
		data.right = new FormAttachment(methodConfig, 100, SWT.LEFT);
		data.bottom = new FormAttachment(methodConfig, 30, SWT.TOP);
		methodConfig.setLayoutData(data);
	}
	
	private void addListenerForMethodconfigButton() {
		methodConfig.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				TreeItem[] treeItems =  treeViewer.getTree().getSelection();
				if (treeItems.length > 0) {
					if (treeItems[0].getData() instanceof MethodTree) {
						MethodTree methodNode = (MethodTree) treeItems[0].getData(); 
						MethodConfigDialog methodConfigDialog = 
								new MethodConfigDialog(shell, (RestWebServiceMethod) methodNode.getData(), RestWebServiceSelectionUI.this);
						methodConfigDialog.open();
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}
		});
	}
	
	@Override
	public void initData(WebServiceDeploy webServiceDeploy) {
		this.webServiceDeploy = webServiceDeploy;
		java.util.List<Itree> trees = new ArrayList<Itree>();
		Itree webService = LocalRemoteTree.createWebServiceTreeNode();
		trees.add(webService);
		copyDeploy(webService,webServiceDeploy);
		treeViewer.setInput(trees);
		treeViewer.expandToLevel(2);
		treeViewer.refresh();
	}

	private void copyDeploy(Itree tree,WebServiceDeploy webServiceDeploy) {
		logger.info("解析成功，初始化数据"+webServiceDeploy);
		List<InterfaceObj> interfaces = webServiceDeploy.getInterfaces();
		for (InterfaceObj inter : interfaces) {
			InterfaceTree interfaceTree = new InterfaceTree(inter.getName());
			for (WebServiceMethod method : inter.getMethods()) {
				RestWebServiceMethod restMethod = (RestWebServiceMethod) method;
				MethodTree methodTree = new MethodTree(JavaManagerUtil.methodDescription(restMethod.getMethod()));
				methodTree.setType(restMethod.getType());
				methodTree.setData(restMethod);
				interfaceTree.getChildren().add(methodTree);
			}
			tree.getChildren().add(interfaceTree);
		}
	}
	
	@Override
	public void process() {
		Map<String,List<String>> selectedMap = new HashMap<String,List<String>>();
		Map<String,List<MethodTree>> selectedTrees = new HashMap<String,List<MethodTree>>();
		List<Itree> trees = (List)this.treeViewer.getInput();
		for(Itree tree:trees){
			List<InterfaceTree> interfaces = (List<InterfaceTree>)tree.getChildren();
			for(InterfaceTree interTree:interfaces){
				if(interTree.isCheck()){
					selectedMap.put(interTree.getName(), interTree.getSelectedChildren());
					selectedTrees.put(interTree.getName(), interTree.getSelectedTree());
				}
			}
		}
		
		List<InterfaceObj> interfaces = new ArrayList<InterfaceObj>(webServiceDeploy.getInterfaces());
		
		for(InterfaceObj inter : interfaces){
			String name = inter.getName();
			List<String> methods = new ArrayList<String>();
			if(selectedMap.containsKey(name)){
				methods = selectedMap.get(name);
				List<MethodTree> methodTrees = selectedTrees.get(name);
				for(WebServiceMethod method : inter.getMethods()){
					RestWebServiceMethod restMethod = (RestWebServiceMethod) method;
					for(MethodTree tree:methodTrees){
						if(restMethod.getName().equals(tree.getName()))restMethod.setType(tree.getType());
					}
				}
				inter.setSelectedMethods(methods);
			}else{
				webServiceDeploy.getInterfaces().remove(inter);
			}
		}
		action.setWebServiceDeploy(webServiceDeploy);
		super.close();
		action.process();
	}
	
	public void refreshMethodLabel(String methodType, String methodName) {
		TreeItem[] treeItems =  treeViewer.getTree().getSelection();
		if (treeItems.length > 0) {
			treeItems[0].setText("[" + methodType + "]" + methodName);
		}
	}
}
